<template>
	<view>
		<course-list :item="item" type="one" :disable ="true"></course-list>
		<uni-list>
			<uni-list-item title="优惠券" showArrow="true" clickable @click="goCouponList">
				<view slot='footer'>
					<view class="font-sm font-weight-bold">
						{{couponShow}}
					</view>
				</view>
			</uni-list-item>
			<uni-list-item title="支付方式">
				<view slot='footer'>
					<view class="font text-success ">
						微信支付
					</view>
				</view>
			</uni-list-item>
		</uni-list>
		<view style="height: 75px;" />
		<view class="buy">
			<view class=" p-2 border-top fixed-bottom bg-white">
				<main-button v-if="item.price" @click="submit">立即购买{{price}}</main-button>
			</view>
		</view>
	</view>
</template>

<script>
	import $tool from "@/common/tool.js"
	export default {
		computed: {
			couponShow() {
				if (this.coupon_price) {
					return `减${this.coupon_price}元`
				}
				return this.couponCount ? `请选择优惠券 (${this.couponCount}张)` : '暂无优惠券'
			},
			price() {
				let p = ((this.item.price * 1000 - this.coupon_price * 1000) / 1000).toFixed(2)
				return p
			}
		},
		data() {
			return {
				type: '',
				id: 0,
				item: {
					"id": 0,

					"title": "",

					"cover": "",

					"price": 0,

					"type": "video"
				},
				couponCount: 0,
				coupon_price: 0,
				user_coupon_id: 0
			}
		},
		onLoad(e) {
			if (!e.type || !e.id) {
				return this.$toast('参数错误！')
			}
			this.type = e.type
			this.id = e.id
			this.getData()
			uni.$on('chooseCoupon', this.handleChooseCoupon)
		},
		beforeDestroy() {
			uni.$off('chooseCoupon', this.handleChooseCoupon)
		},
		methods: {
			getData() {
				uni.showLoading({
					title: 'loading···'
				})
				this.$api.getGoodsList({
					type: this.type,
					id: this.id
				}).then(res => {
					this.item = res
					console.log(this.item, 'this.item');
				}).finally(() => {
					uni.hideLoading()
					this.getCouponList()
				})
			},
			goCouponList() {
				if (!this.couponCount) return
				this.$authJump(`/pages-user/my-coupon/my-coupon?type=${this.type}&goods_id=${this.id}`)
			},
			getCouponList() {
				uni.showLoading({
					title: 'loading···'
				})
				this.$api.getUsableCoupon({
					type: this.type == 'course' ? 'course' : "column",
					goods_id: this.id
				}).then(res => {
					this.couponCount = res
				}).finally(() => {
					uni.hideLoading()
				})
			},
			handleChooseCoupon({
				user_coupon_id,
				price
			}) {
				this.user_coupon_id = user_coupon_id
				this.coupon_price = price
			},
			submit() {
				uni.showLoading({
					title: '创建订单中···'
				})
				let data ={
					goods_id: this.id,
					type: this.type,
					user_coupon_id: this.user_coupon_id
				}
				let type ='save'
				console.log(data,'data');
				if(this.type === 'flashsale') {
					data ={
						flashsale_id :this.id
					}
					type ='flashsale'
				}
				this.$api.createOrder(data).then(res => {
					// h5支付
					// #ifdef H5
					this.$navigateTo('/pages/h5pay/h5pay?no=' + res.no)
					// #endif

					// app端||小程序 支付
					// #ifdef APP-PLUS || MP
					$tool.wxpay(res.no, () => {
						uni.navigateBack({
							delta: 1
						});
					})
					// #endif
					
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>

<style>

</style>
